﻿using Cms.Application.Admin.Views;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class EditUserGroupPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IEditUserGroupView _view;

        public EditUserGroupPresenter(IEditUserGroupView view)
            : this(view, new RepositoryFactory())
        {
        }

        public EditUserGroupPresenter(IEditUserGroupView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.EditUserGroup)
                _view.RedirectToPermissionDeniedPage();
        }
        
        public void Initialize()
        {
            _view.SetLoginFolders(_administrator.CurrentSite.Domain, _administrator.CurrentSite.GetRootFolders());
            _view.SetLoginPages(_administrator.CurrentSite.GetRootPages());
            _view.SetStartFolders(_administrator.CurrentSite.Domain, _administrator.CurrentSite.GetRootFolders());
            _view.SetStartPages(_administrator.CurrentSite.GetRootPages());
            if (_view.GroupID == -1)
            {
                _view.SelectedLoginPageID = -1;
                _view.SelectedStartPageID = -1;
                return;
            }
            RefreshProperties();
        }

        public void RefreshProperties()
        {
            if (_view.GroupID < 0)
            {
                return;
            }
            UserGroup group = UserRepository.GetUserGroup(_view.GroupID);
            _view.Name = group.Name;
            if (group.LoginPage != null)
            {
                if (group.LoginPage.Folder == null)
                {
                    _view.SelectedLoginFolderID = -1;
                }
                else
                {
                    _view.SelectedLoginFolderID = group.LoginPage.Folder.ID;
                    _view.SetLoginPages(group.LoginPage.Folder.Pages);
                }
                _view.SelectedLoginPageID = group.LoginPage.ID;
            }
            else
            {
                _view.SelectedLoginPageID = -1;
            }
            if (group.StartPage != null)
            {
                if (group.StartPage.Folder == null)
                {
                    _view.SelectedStartFolderID = -1;
                }
                else
                {
                    _view.SelectedStartFolderID = group.StartPage.Folder.ID;
                    _view.SetStartPages(group.StartPage.Folder.Pages);
                }
                _view.SelectedStartPageID = group.StartPage.ID;
            }
            else
            {
                _view.SelectedStartPageID = -1;
            }
        }

        public void UpdateLoginPages()
        {
            _view.SetLoginPages(_view.SelectedLoginFolderID == -1
                                    ? _administrator.CurrentSite.GetRootPages()
                                    : FolderRepository.GetFolder(_view.SelectedLoginFolderID).Pages);
        }
        
        public void UpdateStartPages()
        {
            _view.SetStartPages(_view.SelectedStartFolderID == -1
                                    ? _administrator.CurrentSite.GetRootPages()
                                    : FolderRepository.GetFolder(_view.SelectedStartFolderID).Pages);
        }

        public void Save()
        {
            if (_view.Name.Trim() == "")
                AddValidationError(GetText(_view, "NoNameError"));
            if (!IsValid)
                return;
            UserGroup group = _view.GroupID == -1 ? new UserGroup() : UserRepository.GetUserGroup(_view.GroupID);
            group.Name = _view.Name;
            group.LoginPage = PageRepository.GetPage(_view.SelectedLoginPageID);
            group.StartPage = PageRepository.GetPage(_view.SelectedStartPageID);
            UserRepository.SaveUserGroup(group);
            _view.SetGroupID(group.ID);
        }
    }
}